package org.zjx.analysis.window;

import lombok.Getter;
import org.zjx.analysis.BaseAnalysis;
import org.zjx.analysis.inventor.PropAnalysis;
import org.zjx.core.IWindow;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

@Getter
public class ShoppingAnalysis extends BaseAnalysis {
    private PropAnalysis propAnalysis;
    private List<BufferedImage> itemImages;
    private int buyCount;
    private int buyMoney;

    public ShoppingAnalysis(IWindow window, BufferedImage screenshot) {
        super(window, screenshot);
    }

    @Override
    protected void analysis() {
        BufferedImage contentImage = ImageUtil.cutImage(screenshot,conversion(windowArea,buyContentArea()));
        propAnalysis = new PropAnalysis(contentImage);
        itemImages = propAnalysis.getItemImages();

        //输入的购买数量
        String buyCountStr = identifyTess(conversion(windowArea,areaMap.get("数量").getRectangle()));
        buyCount = buyCountStr.isEmpty()?0:Integer.parseInt(buyCountStr);

        //输入的购买数量
        String buyMoneyStr = identifyTess(conversion(windowArea,areaMap.get("总价").getRectangle()));
        buyMoney = buyMoneyStr.isEmpty()?0:Integer.parseInt(buyMoneyStr);
    }

    public Rectangle getItemIndexArea(int index){
        return conversion(windowArea,conversion(buyContentArea(),propAnalysis.getIndexArea(index)));
    }

    @Override
    protected void initResources() {
        areaMap.put("单价", new ResolutionArea(ImageUtil.createRect(105,245,207,262)));
        areaMap.put("数量", new ResolutionArea(ImageUtil.createRect(105,273,207,290)));
        areaMap.put("总价", new ResolutionArea(ImageUtil.createRect(105,301,207,318)));
        areaMap.put("现金", new ResolutionArea(ImageUtil.createRect(105,329,207,346)));
    }

    public Rectangle buyContentArea(){
        return ImageUtil.createRect(7,27,262,231);
    }

    public Rectangle buyButtonArea(){
        return ImageUtil.createRect(86,362,183,381);
    }

    @Override
    public Rectangle closeArea() {
        return ImageUtil.createRect(251,6,260,15);
    }

    @Override
    public Rectangle getOriginBounds() {
        return ImageUtil.createRect(265,102,534,496);
    }

    @Override
    public Rectangle getRefImageArea() {
        return ImageUtil.createRect(119,3,150,16);
    }

    @Override
    public BufferedImage getRefImage() {
        return ImageUtil.createImage("iVBORw0KGgoAAAANSUhEUgAAACAAAAAOCAYAAABO3B6yAAADCElEQVR4XpVUi09SYRTnH2uJ5aOyVATRSEXLUKdzauhYLbRga6mhOEUN5DJT8JGmqQ1BBZ/5aHPLbD7ykdNcPnJqqbN5ut+53gtc6HW2387r953zu983EFwQPQc+LooZpKs6IExm5vK/gXAjU+r/iOtyBsGxJtwlIAf55hxaxIHEHpf3+y36Hf7XriZSIBClNmByR9kG0gwb2F3zmIfE13lzOdvdO6IPWiBIYkQIxbVcfCnOBCU1Q9DSPQPhCRSE36IgW90F2goXxgTkllRP7DgrWGoEgfhuPSZpBe3gHFwAqmkK8wrTCHpj4yQYrVMIS8s7rN2nB5DFgaAu7UMOsaScVvygrZ1DHw654aPjU4w5AcUGN3pWALHBt0uQnNPic5iYt4AgcTUXT3/YAHWJAyISzTA0vgw56tfnAr77zNDQz5qv6cGznICnBhcjoHkSfXffRzA3MbFQUs2BWL62m8tRwHlsd81hf21jz1NzzzECvGaw5wIKYJeS5sLyNmxuHUCvex6KdA6sEct88IobwIcs0wp60zCXE1FEAJ/HCZCkUji0uLIfPWWbQC8UVaKvrR+D4fEl2D845mrJ2VaMvaF81Amf17/h13uDNX6dcPMKOzwCDNQInJ15BLCClEWdMDa1gnGU3MR5vgDFvWZwjy7C4NgnHxA7OfnpV3fR3Nu5NhDcSKhB0uT0KiytbtOLnbD5dR9W1naw7m1lNQPohdH6f4JWx/zc8tTt9EcsQ7yC8uMIhNE6iJAxV9vV+x59TIoRIU2t44i5D9vo52B+mvwhgWBqGDufOQNX4qro/48fmNsHZuGyuBzIXgIUMDv/BZuhsXr0xyen2IxXmKC0yglRSdUQJtXjE61v7nGH+QgW6egbdOAMYg0vJ3z6Gt0brpembGQEaHU9WFBp2iAosgQUeS8wl6XX0Vfu+VNhLaPA6reYP5w8nzzLgvO8OSS/drMCdnYPkUd2CkIlZTTZjE0WITEMmY2zVFZQFrZCuLTch+cNeaYZLLZRiEqs9OsFwjODg37iWvgFzZbw1o19+NQAAAAASUVORK5CYII=");
    }
}
